package com.google.code.iproc;

import java.io.IOException;

import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Font;
import com.itextpdf.text.pdf.BaseFont;

public class FontFactory {
	
	public Font getArial( int size ) throws DocumentException, IOException {
		String fontName;
		if ( isWindows() ) {
			fontName = "arial.ttf";
		} else if ( isMac() ) {
			fontName = "Arial.ttf";
		} else {
			throw new IllegalStateException( "Unsupported platform (os.name=" + System.getProperty( "os.name" ) + ")" );
		}
		return new Font( BaseFont.createFont( getFontsDir() + fontName, BaseFont.IDENTITY_H, BaseFont.EMBEDDED ), size );
	}
	
	public Font getArialBold( int size ) throws DocumentException, IOException {
		String fontName;
		if ( isWindows() ) {
			fontName = "arialbd.ttf";
		} else if ( isMac() ) {
			fontName = "Arial Bold.ttf";
		} else {
			throw new IllegalStateException( "Unsupported platform (os.name=" + System.getProperty( "os.name" ) + ")" );
		}
		return new Font( BaseFont.createFont( getFontsDir() + fontName, BaseFont.IDENTITY_H, BaseFont.EMBEDDED ), size );
	}
	
	public Font getTimesNewRoman( int size ) throws DocumentException, IOException {
		String fontName;
		if ( isWindows() ) {
			fontName = "times.ttf";
		} else if ( isMac() ) {
			fontName = "Times New Roman.ttf";
		} else {
			throw new IllegalStateException( "Unsupported platform (os.name=" + System.getProperty( "os.name" ) + ")" );
		}
		return new Font( BaseFont.createFont( getFontsDir() + fontName, BaseFont.IDENTITY_H, BaseFont.EMBEDDED ), size );
	}
	
	public Font getTimesNewRomanItalic( int size ) throws DocumentException, IOException {
		String fontName;
		if ( isWindows() ) {
			fontName = "timesi.ttf";
		} else if ( isMac() ) {
			fontName = "Times New Roman Italic.ttf";
		} else {
			throw new IllegalStateException( "Unsupported platform (os.name=" + System.getProperty( "os.name" ) + ")" );
		}
		return new Font( BaseFont.createFont( getFontsDir() + fontName, BaseFont.IDENTITY_H, BaseFont.EMBEDDED ), size );
	}
	
	private String getFontsDir() {
		if ( isWindows() )
			return "C:\\Windows\\Fonts\\";
		if ( isMac() )
			return "/Library/Fonts/Microsoft/";
		// ...
		throw new IllegalStateException( "Unsupported platform (os.name=" + System.getProperty( "os.name" ) + ")" );
	}
	private boolean isWindows() {
		return "Windows 7".equals( System.getProperty( "os.name" ) );
	}
	
	private boolean isMac() {
		return "Mac OS X".equals( System.getProperty( "os.name" ) );
	}
}
